package cn.anlab.anappframe.net.sub;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.util.Log;

public class AsyncRequest {

	private static ThreadPoolExecutor threadPool;
	private InetSocketAddress address;
	
	public AsyncRequest(InetSocketAddress address) {
		this.address = address;
	}

	public void setConNumPerNIOThread(int count) {

	}

	public void setBody(byte[] body) {

	}

	public void startAsyn(int id) {
		try {
			TCPConnection conn= new TCPConnection("0", address);
			threadPool.submit(conn);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
//		Selector selector = Selector.open();
//		channel.register(selector, SelectionKey.OP_CONNECT);
//		int n = 0;
//		try {
//			 if(selector != null)
//			      n = selector.select(3000);
//			 // 如果要shutdown，关闭selector退出
//			 if (shutdown) {
//			      selector.close();
//			      break;                  
//			  }
//			} catch (IOException e) {
//			    dispatchErrorToAll(e);
//			}            
//			// 如果select返回大于0，处理事件
//			if(n > 0) {
//			 for (Iterator<SelectionKey> i = selector.selectedKeys().iterator(); i.hasNext();) {
//			     // 得到下一个Key
//			     SelectionKey sk = i.next();
//			     i.remove();
//			     // 检查其是否还有效
//			     if(!sk.isValid())
//			         continue;
//			 
//			      // 处理
//			      INIOHandler handler = (INIOHandler)sk.attachment();
//			      try {
//			            if(sk.isConnectable())
//			            {
//			                handler.processConnect(sk);
//			            }
//			            else if (sk.isReadable())
//			            {
//			                handler.processRead(sk);
//			            }
//			            } catch (IOException e) {
//			                handler.processError(e);
//			            } catch (RuntimeException e) {
//			             }
//			        }
//			             n = 0;
//			         }
//			         checkNewConnection();
//			         notifySend();    

		
	}

	public boolean isCurrRequestFull() {
		return false;
	}
	
	public void setUDP(boolean b) {
		
	}

	public static synchronized ThreadPoolExecutor setThreadPoolNum(int aThreadPoolMinNum, int aThreadPoolMaxNum, long keepAliveTime) {
		if (threadPool == null) {
			threadPool = new ThreadPoolExecutor(aThreadPoolMinNum, aThreadPoolMaxNum, keepAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
					new ThreadPoolExecutor.DiscardOldestPolicy());
		}

		return threadPool;
	}

	public static void main(String[] args) {
		AsyncRequest request = new AsyncRequest(new InetSocketAddress("10.0.2.2", 8042));
		try {
			request.setConNumPerNIOThread(5);
			AsyncRequest.setThreadPoolNum(2, 5, 2);
			request.setUDP(false);
			request.setBody("hello world".getBytes());
			request.startAsyn(0);

			if (!request.isCurrRequestFull()) {
				request.setBody("My name is dongfengsun".getBytes());
				request.startAsyn(1);
			}
		} catch (Exception e) {
			Log.e("onCreate", " = " + e.getMessage());
			e.printStackTrace();
		}
	}

}
